
#include <allegro.h>
#include <stdlib.h>
#include <stdio.h>
#include<time.h>
#include "opening.h"
#include "clavier.h"
#include "menu.h"
void niveau_gauche(picture *pic,music mus,int avancer,int *poskarhba,positions position)

{
    int i=0;
    if (avancer ==10000)
    {
        pic->karhba=load_bitmap("karhba7.bmp",NULL);
        pic->karhba2=load_bitmap("karhba8.bmp",NULL);
        stop_sample(mus.sample2);
        play_sample(mus.sample, 255, 128, 1000, FALSE);
        *poskarhba=SCREEN_H-250;




    }
    if (avancer==20000)
    {


        pic->karhba2=load_bitmap("7.bmp",NULL);
        pic->karhba=load_bitmap("chameau6.bmp",NULL);
        stop_sample(mus.sample3);
        play_sample(mus.sample2, 255, 128, 1000, FALSE);

        *poskarhba=SCREEN_H-300;


    }
    if ( avancer <2000)

    {
        draw_sprite(pic->Buffer, pic->Imagele,position.x1,position.y1 );
        stop_sample(mus.sample2);
        play_sample(mus.sample, 255, 128, 1000, FALSE);
        *poskarhba=SCREEN_H-250;
    }


    else
    {
        draw_sprite(pic->Buffer, pic->karhba,position.x1,*poskarhba);
    }
}
/**************************************************************************************************/
void niveau_droit(int avancer,picture *pic,music mus,positions position,int *poskarhba)
{

    if (avancer <2000)
    {
        draw_sprite(pic->Buffer,pic->Imageri,position.x1,position.y1 );/*mil lowel nsawrouh yimchi 3la s9ih*/
        *poskarhba=SCREEN_H-250;

    }
    else
    {
        draw_sprite(pic->Buffer, pic->karhba2,position.x1,*poskarhba);/*mba3ed ywali 3al karhba w ala jmal*/
    }
    if (avancer ==9500)
    {
        rest(400);
        while(!key[KEY_ENTER])
         {blit(pic->mission[1],pic->Buffer,0,0, 0, 0, pic->mission[1]->w, pic->mission[1]->h);
     blit(pic->Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
         }
    rest(2000);
        pic->karhba=load_bitmap("chameau6.bmp",NULL);
        pic->karhba2=load_bitmap("chameau5.bmp",NULL);
        stop_sample(mus.sample);
        play_sample(mus.sample2, 255, 128, 1000, FALSE);
        *poskarhba=SCREEN_H-300;





    }
    if (avancer==19500)
    {
                while(!key[KEY_ENTER])
         {blit(pic->mission[1],pic->Buffer,0,0, 0, 0, pic->mission[1]->w, pic->mission[1]->h);
     blit(pic->Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
         }
          rest(2000);
        *poskarhba=SCREEN_H-600;


        pic->karhba=load_bitmap("flouka26.bmp",NULL);
        pic->karhba2=load_bitmap("flouka27.bmp",NULL);
        stop_sample(mus.sample2);
        play_sample(mus.sample3, 255, 128, 1000, FALSE);
        play_sample(mus.sample4,255, 128, 1000, TRUE);


    }
}
/**************************************************************************************************/
void gauche (picture *pic,int *avancer ,positions position,int *poskarhba,cais dinar [],vi v[],int *money,music mus)
{
    int i=0;
    clear_bitmap(pic->Buffer); /*nfas5ou kol chay ili ken msawer bich mayibdewech el tsawer fou9 b3adhehom*/
    blit(pic->Background, pic->Buffer, *avancer, 300, 0, 0, pic->Background->w, pic->Background->h);/*nibdew nsawrou 3al buffer*/
    if (*avancer < 2000)

    {
        draw_sprite(pic->Buffer,pic->Imagel,position.x1,position.y1 ); /*mil lowel nsawrouh yimchi 3la s9ih*/
        *poskarhba=SCREEN_H-250;
    }
    else

    {

        draw_sprite(pic->Buffer, pic->karhba,position.x1,*poskarhba);/*mba3ed ywali 3al karhba w ala jmal*/
    }
    for (i=0; i<4; i++)


    {
        draw_sprite(pic->Buffer, pic->vie,i*60,0 ); /*naswarou fazet*/
    }

    for(i=0; i<20; i++)
    {

        if (dinar[i].x!=SCREEN_W/2+50)
        {
            draw_sprite(pic->Buffer, pic->caisse,dinar[i].x,SCREEN_H/2+50 );

        }
        else if (dinar[i].x==SCREEN_W/2+50)
        {
              *money=*money+10;
            dinar[i].x=-80000;


        }
    }
    for(i=0; i<20; i++)
    {
        if (v[i].x!=SCREEN_W/2-50)
        {
            draw_sprite(pic->Buffer, pic->vie,v[i].x,SCREEN_H/2+50 );
        }
        else
        {
            v[i].x=-80000;
        }


    }


    image_fixe(*pic);
    blit(pic->Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

    rest(20);/*pause 20sc */
    clear_bitmap(pic->Buffer);/*ba3ed n9ademou el background */
    *avancer=*avancer-50;
    blit(pic->Background, pic->Buffer,*avancer, 300, 0, 0, pic->Background->w, pic->Background->h);
    niveau_gauche(pic,mus,*avancer,poskarhba,position);



    for (i=0; i<4; i++)
    {
        draw_sprite(pic->Buffer, pic->vie,i*60,0 );
    }
    for(i=0; i<20; i++)
    {
        v[i].x=v[i].x+50;

            draw_sprite(pic->Buffer, pic->vie,v[i].x,SCREEN_H/2+50 );



    }

    for(i=0; i<20; i++)
    {
        dinar[i].x=dinar[i].x+50;
            draw_sprite(pic->Buffer, pic->caisse,dinar[i].x,SCREEN_H/2+50 );





    }









    image_fixe(*pic);

    blit(pic->Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}
/**************************************************************************************************/
void droite (int *avancer ,positions position, picture *pic,vi v[],cais dinar[],int *money,music mus,int *poskarhba,int *nd,int *nv  )

{
    int i=0,j=0;


    clear_bitmap(pic->Buffer);/*nfas5ou kol chay ili ken msawer bich mayibdewech el tsawer fou9 b3adhehom*/

    blit(pic->Background, pic->Buffer, *avancer, 300, 0, 0, pic->Background->w, pic->Background->h);
    /*nibdew nsawrou 3al buffer*/
    textprintf_ex(pic->Buffer,font,300,200,makecol(150,155,125),0,"ena 7oulaaaaaaaaaaaaaa");
    niveau_droit(*avancer,pic,mus,position,poskarhba);

    for (i=0; i<4; i++)
    {
        draw_sprite(pic->Buffer, pic->vie,i*60,0 );/*naswarou fazet*/
    }
for(i=0; i< *nv; i++)
    {

draw_sprite(pic->Buffer, pic->vie,v[i].x,SCREEN_H/2+50 );
    }
    for (i=0;i< *nd;i++)
    {

            draw_sprite(pic->Buffer, pic->caisse,dinar[i].x,SCREEN_H/2+50 );
    }













    image_fixe(*pic);

    blit(pic->Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
    rest(20);
    /*pause 20sc */

    /*nfas5ou bich nsawrou backgound m9adem */



     if(getpixel(pic->Backgroundcol,*avancer-400,SCREEN_H-250)!=makecol(255,255,255))
     {
      clear_bitmap(pic->Buffer);
    *avancer=*avancer+50;
    blit(pic->Background, pic->Buffer, *avancer, 300, 0, 0, pic->Background->w, pic->Background->h);
    textprintf_ex(pic->Buffer,font,300,200,makecol(150,155,125),0,"ena 7oulaaaaaaaaaaaaaa");

    if (*avancer<2000)
    {
        draw_sprite(pic->Buffer, pic->Image,position.x1,position.y1 );
        *poskarhba=SCREEN_H-250;
    }
    else
    {
        draw_sprite(pic->Buffer, pic->karhba2,position.x1,*poskarhba);


    }
    for (i=0; i<4; i++)
    {
        draw_sprite(pic->Buffer, pic->vie,i*60,0 );
    }
   for (i=0;i<*nd;i++)

      {
          dinar[i].x=dinar[i].x-50;

       if (dinar[i].x>SCREEN_W/2)
        {
            draw_sprite(pic->Buffer, pic->caisse,dinar[i].x,SCREEN_H/2+50 );
        }
        else if (dinar[i].x<=SCREEN_W/2)
        {
           for (j=i;j<*nd;j++)

            {dinar[j].x=dinar[j+1].x;
            }
              *nd=*nd-1;
              *money=*money+10;
        }
       }




    for(i=0; i<*nv; i++)
    {
       v[i].x=v[i].x-50;

        if (v[i].x>=SCREEN_W/2)
        {
            draw_sprite(pic->Buffer, pic->vie,v[i].x,SCREEN_H/2+50 );
        }
        else if (v[i].x<SCREEN_W/2)
        {
            for(j=i;j<*nv;j++)
            {
                v[j].x=v[j+1].x;

            }
            *nv=*nv-1;
        }
    }







    textprintf_ex(pic->Buffer,font,100,85,makecol(255,255,255),0,"%d",*money);
    image_fixe(*pic);




textprintf_ex(pic->Buffer,font,300,200,makecol(150,155,125),0,"ena 7oulaaaaaaaaaaaaaa");
    blit(pic->Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

     }
}
/**************************************************************************************************/
void sauter (picture pic,positions position,int poskarhba,int *avancer,int *by)
{
    int i=0,j=0;



    for(j=0; j<45; j++)
    {

        blit(pic.Background,pic.Buffer,*avancer,*by,0,0, pic.Background->w,pic.Background->h);


        draw_sprite(pic.Buffer,pic.karhba2,position.x1,poskarhba );
        for (i=0; i<4; i++)
        {

            draw_sprite(pic.Buffer, pic.vie,i*60,0 );
        }





        image_fixe(pic);

        blit(pic.Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);

        *avancer=*avancer+10;
        *by=*by-6;

        blit(pic.Background, pic.Buffer,*avancer,*by, 0, 0, pic.Background->w, pic.Background->h);
        draw_sprite(pic.Buffer, pic.karhba2,position.x1,poskarhba );

        for (i=0; i<4; i++)
        {
            draw_sprite(pic.Buffer, pic.vie,i*60,0 );
        }




        image_fixe(pic);

        blit(pic.Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);



        clear_bitmap (pic.Buffer);
    }
    for(j=0; j<45; j++)
    {

        *avancer=*avancer+10;
        *by=*by+6;

        blit(pic.Background, pic.Buffer,*avancer,*by, 0, 0, pic.Background->w, pic.Background->h);
        draw_sprite(pic.Buffer,pic.karhba2,position.x1,poskarhba);

        for (i=0; i<4; i++)
        {
            draw_sprite(pic.Buffer, pic.vie,i*60,0 );
        }




        image_fixe(pic);

        blit(pic.Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);


        blit(pic.Buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
        clear_bitmap (pic.Buffer);


    }
}
